<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>摇号信息</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-row>
          <el-col :span="24" class="warp-main">
            <el-form ref="infoForm" :model="infoForm" label-width="100px">
              <el-form-item label="抽签状态:" prop="name" v-if="detailOption">
              <span
                :style="{'color':detailOption.is_end?'green':'red','fontSize': '18px','fontWeight':'block'}">{{
                  detailOption.is_end ? '已开奖' : '未开奖'
                }}</span>
              </el-form-item>
              <el-form-item label="摇号事项:" prop="name">
                <el-input v-model="infoForm.name" placeholder="填写摇号事项" :readonly="pageId?true:false"
                          size="small"></el-input>
              </el-form-item>
              <el-form-item label="盲号总数:" prop="name">
                <el-input v-model="infoForm.number" placeholder="填写盲号总数" :readonly="pageId?true:false"
                          size="small"></el-input>
              </el-form-item>
              <el-form-item label="参与人身份:">
                <el-radio-group v-model="infoForm.type" :disabled="pageId?true:false" @change="changeRadio">
                  <el-radio :label="1">教职工</el-radio>
                  <el-radio :label="0">家长</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="参与人:" v-if="infoForm.type==1">
                <el-button @click="chooseStudent" class="mb-10" size="small" v-if="!pageId">选择参与人 （共选
                  {{ dynamicTags.length }} 人）
                </el-button>
                <el-button @click="uservisible=true" class="mb-10" size="small" v-else>查看参与人</el-button>
                <div>
                  <el-tag
                    :key="tag"
                    v-for="(tag,index) in dynamicTags"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(index)"
                    class="mr-10"
                  >{{ tag }}
                  </el-tag>
                </div>
              </el-form-item>
              <el-form-item label="参与人:" v-else>
                <el-upload
                  class="upload-demo"
                  :action="$uploadImg"
                  :on-remove="handleRemove"
                  :on-success="handleSuccess"
                  :before-upload="beforeAvatarUpload"
                  accept=".xls,.xlsx"
                  multiple
                  :limit="1"
                  v-if="!pageId">
                  <el-button size="small" class="el-icon-upload">点击上传相应文件</el-button>
                  <div slot="tip" class="el-upload__tip">上传excel文件</div>
                </el-upload>
                <el-button @click="uservisible=true" class="mb-10" size="small" v-else>查看参与人</el-button>
              </el-form-item>
              <el-form-item label="盲号安排:" prop="descript">
                <el-button @click="addGiftFn" class="mb-10" size="small" icon="el-icon-plus" v-if="!pageId">添加奖项安排
                </el-button>
                <div class="draw-body">
                  <div class="draw-body-list mb-10" v-for="(item,index) in giftList" :key="index">
                    <div class="draw-body-line">
                      <span>奖项安排：</span>
                      <el-input v-model="item.name" placeholder="开盲结果" size="small"
                                :readonly="pageId?true:false"></el-input>
                    </div>
                    <div class="draw-body-line mr-10">
                      <span>盲号数：</span>
                      <el-input v-model="item.number" placeholder="盲号总数" :readonly="pageId?true:false" size="small"
                                class="w-80"></el-input>
                    </div>
                    <div class="draw-body-line draw-body-line-btn" v-if="!pageId">
                      <i class="el-icon-remove-outline" @click="detelGiftLine(index)"></i>
                    </div>
                    <div v-else class="draw-body-line draw-body-line-btn ml-30">
                      <el-button @click="showGiftLine(index)" size="small" :readonly="detailOption.is_end?false:true">
                        查看摇中记录
                      </el-button>
                    </div>
                  </div>
                </div>
              </el-form-item>
              <!--            <el-form-item>-->
              <!--              <el-button type="primary" @click="onSubmit" size="small">确认并提交</el-button>-->
              <!--            </el-form-item>-->
            </el-form>
          </el-col>
        </el-row>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">返回</el-button>
        <el-button @click.native="onSubmit()" size="small" type="primary" plain>确定并保存</el-button>
      </div>
    </div>
    <MultipleTeacherModle :visible.sync="studentVisible" @studentFn="studentFn" :userclassId="infoForm.class_id"
                          :usersectionId="infoForm.section_id"
                          :dynamicTagsArray="dynamicTagsArray"></MultipleTeacherModle>
    <!--    <teacherMultiple :visible.sync="studentVisible" @studentFn="studentFn" :userclassId="infoForm.class_id"-->
    <!--                     :usersectionId="infoForm.section_id" :dynamicTagsArray="dynamicTagsArray"></teacherMultiple>-->
    <lookuser :visible.sync="uservisible" :id="pageId"></lookuser>
    <giftList :visible.sync="giftvisible" :drawId="pageId" :prizeId="prizeId" :prizeName="prizeName"></giftList>
  </div>
</template>

<script>
import teacherMultiple from '@/components/dialog/teacherMultiple.vue'
import lookuser from '@/components/dialog/giftdraw/lookuser'
import giftList from '@/components/dialog/giftdraw/giftList'

export default {
  props: ['visible', 'pageId'], // recruitmentType examine 审核
  data() {
    return {
      dialogVisible: false,
      infoForm: {
        name: '', // 	整型 	必填 	1 	- 	名称
        type: 1, // 	整型 	必填 	1 	- 	类型0家长使用1老师使用
        file: '', // 	整型 	必填 	1 	- 	家长使用时传文件
        number: '', // 	整型 	必填 	1 	- 	奖品总数量
        user_ids: '', // 	整型 	必填 	1 	- 	老师使用时参与老师id
        lists: '' // 	字符串 	必填 	1 	- 	[{"name":"奖品名称","number":10},{"name":"奖品名称","number":5}]
      },
      giftList: [],
      // 限制时间选择
      studentVisible: false,
      dynamicTags: [],
      dynamicTagsId: [],
      dynamicTagsArray: {},
      uservisible: false,
      giftvisible: false,
      prizeId: 0,
      detailOption: null,
      prizeName: ''
    }
  },
  components: {
    teacherMultiple,
    lookuser,
    giftList
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.pageId) {
          this.getDetailFn()
        }
      } else {
        this.infoForm = {
          name: '', // 	整型 	必填 	1 	- 	名称
          type: 1, // 	整型 	必填 	1 	- 	类型0家长使用1老师使用
          file: '', // 	整型 	必填 	1 	- 	家长使用时传文件
          number: '', // 	整型 	必填 	1 	- 	奖品总数量
          user_ids: '', // 	整型 	必填 	1 	- 	老师使用时参与老师id
          lists: '' // 	字符串 	必填 	1 	- 	[{"name":"奖品名称","number":10},{"name":"奖品名称","number":5}]
        }
        this.giftList = []
      }
    }
  },
  computed: {},
  created() {
  },
  mounted() {
  },
  methods: {
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getDetailFn() {
      this.$fetch(this.api.giftDraw.edit, {id: this.pageId}).then(res => {
        if (res.code === 0) {
          this.infoForm.name = res.detail.name
          this.infoForm.type = res.detail.type
          this.infoForm.number = res.detail.number
          this.giftList = res.list
          this.detailOption = res.detail
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    changeRadio() {
      console.log('infoForm.type:', this.infoForm.type) // 类型0家长使用1老师使用
      if (Number(this.infoForm.type) === 0) {
        this.infoForm.user_ids = '';
        this.dynamicTags = [];
        this.dynamicTagsId = [];
        this.dynamicTagsArray = {};
      }
      if (Number(this.infoForm.type) === 1) {
        this.infoForm.file = ''
      }
      // infoForm: {
      //   name: '', // 	整型 	必填 	1 	- 	名称
      //       type: 1, // 	整型 	必填 	1 	- 	类型0家长使用1老师使用
      //       file: '', // 	整型 	必填 	1 	- 	家长使用时传文件
      //       number: '', // 	整型 	必填 	1 	- 	奖品总数量
      //       user_ids: '', // 	整型 	必填 	1 	- 	老师使用时参与老师id
      //       lists: '' // 	字符串 	必填 	1 	- 	[{"name":"奖品名称","number":10},{"name":"奖品名称","number":5}]
      // },
      //   <el-radio-group v-model="infoForm.type" :disabled="pageId?true:false" @change="changeRadio">
      //   <el-radio :label="1">教职工</el-radio>
      //   <el-radio :label="0">家长</el-radio>
      // </el-radio-group>
    },
    onSubmit() {
      // 提交 this.$refs.infoForm.validate，这是表单验证
      console.log('数据提交', this.infoForm)
      if (!this.infoForm.name) {
        this.$message.error('填写摇号名称')
        return
      }
      if (this.infoForm.users === '') {
        this.$message.error('请选择执行人员')
        return false
      }
      console.log('用户信息：', this.dynamicTagsId)
      console.log(this.infoForm)
      console.log(this.giftList)
      let numGift = 0
      let nameSatus = true
      for (let i in this.giftList) {
        numGift += Number(this.giftList[i].number)
        if (!this.giftList[i].name) {
          nameSatus = false
        }
      }

      if (this.dynamicTagsId.length > Number(this.infoForm.number)) {
        this.$message.error('抽奖个数需大于参与人数')
        return
      }
      if (!nameSatus) {
        this.$message.error('请完善抽奖内容')
        return
      }
      if (Number(this.infoForm.number) !== numGift) {
        this.$message.error('抽奖个数与填写内容不符')
        return
      }
      this.infoForm.lists = JSON.stringify(this.giftList)
      this.$post(this.api.giftDraw.add, this.infoForm).then(res => {
        if (res.code === 0) {
          this.$message.success(res.msg)
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    // 添加奖品
    addGiftFn() {
      this.giftList.push({
        name: '',
        number: 1
      })
    },
    // 删除行
    detelGiftLine(index) {
      this.giftList.splice(index, 1)
    },
    chooseStudent() {
      this.studentVisible = true
    },
    studentFn(option) {
      console.log('获取教师信息L:', option)
      this.dynamicTags = []
      this.dynamicTagsId = []
      for (let i in option) {
        this.dynamicTags.push(option[i].realname)
        this.dynamicTagsId.push(option[i].id)
        this.dynamicTagsArray = option
      }
      this.infoForm.user_ids = this.dynamicTagsId.join(',')
    },
    handleClose(index) {
      this.dynamicTags.splice(index, 1)
      delete this.dynamicTagsArray[this.dynamicTagsId[index]] // 去除选中的人
      this.dynamicTagsId.splice(index, 1)
      this.infoForm.user_ids = this.dynamicTagsId.join(',')
    },
    beforeAvatarUpload(file) {
      console.log('上传数据excel：', file)
    },
    handleSuccess(response) {
      this.infoForm.file = response.data.pic
    },
    handleRemove(file, fileList) {
      console.log('移除', file, fileList)
      this.infoForm.appendix = ''
    },
    // 查看中奖记录
    showGiftLine(index) {
      this.prizeName = this.giftList[index].name;
      this.prizeId = this.giftList[index].id;
      this.giftvisible = true;
    }
  }

}
</script>
<style lang="scss">

#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 800px;
  height: auto;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.ql-editor {
  max-height: 250px;
}

.el-dialog__body {
  padding: 0 20px;
}

.el-radio__label {
  /*display: none;*/
}

.warp {
  width: 80%;
  padding: 30px;
  margin: 0 auto;
}

.el-dialog__body {
  /*text-align: center;*/
}

.el-pagination {
  padding-top: 20px;
}

.draw-body-list {
  display: flex;
  align-items: center;
}

.draw-body-line {
  display: flex;
  align-items: center;
}

.draw-body-line:first-child {
  margin-right: 20px;
}

.draw-body-line span {
  width: 100px;
  text-align: right;
}

.draw-body-line-btn i {
  font-size: 26px;
  margin-left: 10px;
  cursor: pointer;
}

</style>
<!--<div class="draw-body-line draw-body-line-btn">-->
<!--<i class="el-icon-remove-outline"></i>-->
<!--</div>-->
